package sis.uitest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-08-04 17:03
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;
import sis.studentinfo.Course;
import sis.ui.CoursesPanel;
import sis.ui.Util;

import static sis.ui.CoursesPanel.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyListener;

public class CoursesPanelTest extends TestCase {

    //面板
    private CoursesPanel panel;


    private boolean wasClicked;

    protected void setUp() {
        panel = new CoursesPanel();
    }

    //测试按钮点击事件
    public void testAddButtonClick() {
        JButton button = panel.getButton(ADD_BUTTON_NAME);
        wasClicked = false;
        //添加点击事件
        panel.addCourseAddListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                wasClicked = true;
            }
        });

        //点击
        button.doClick();
        assertTrue(wasClicked);
    }

    //测试添加内容
    public void testAddCourse() {
        Course course = new Course("ENGL", "101");
        panel.addCourse(course);
        JList list = panel.getList(COURSES_LIST_NAME);
        ListModel model = list.getModel();
        assertEquals("ENGL-101", model.getElementAt(0).toString());
    }

    //测试禁用启用按钮
    public void testEnableDisable() {
        //设置按钮可以点击
        panel.setEnabled(ADD_BUTTON_NAME, true);
        //获取按钮
        JButton button = panel.getButton(ADD_BUTTON_NAME);
        //断言可以点击
        assertTrue(button.isEnabled());
        //设置不可点击
        panel.setEnabled(ADD_BUTTON_NAME, false);
        //断言不可以点击
        assertFalse(button.isEnabled());
    }

    //监听文本框
    public void testAddListener() throws Exception {
        //监听文本框事件
        KeyListener listener = new KeyAdapter() {
        };
        //添加文本框事件
        panel.addFieldListener(DEPARTMENT_FIELD_NAME, listener);
        //获取文本框
        JTextField field = panel.getField(DEPARTMENT_FIELD_NAME);
        //获取文本框事件列表
        KeyListener[] listeners = field.getKeyListeners();
        //断言只有一个事件
        assertEquals(1, listeners.length);
        //断言是同一个事件
        assertSame(listener, listeners[0]);
    }

    //测试面板内容
    public void testCreate() {
        assertEmptyList(COURSES_LIST_NAME);
        assertButtonText(ADD_BUTTON_NAME, ADD_BUTTON_TEXT);
        assertLabelText(DEPARTMENT_LABEL_NAME, DEPARTMENT_LABEL_TEXT);
        assertEmptyField(DEPARTMENT_FIELD_NAME);
        assertLabelText(NUMBER_LABEL_NAME, NUMBER_LABEL_TEXT);
        assertEmptyField(NUMBER_FIELD_NAME);

        JButton button = panel.getButton(ADD_BUTTON_NAME);
        assertEquals(ADD_BUTTON_MNEMONIC, button.getMnemonic());
    }

    private void assertLabelText(String name, String text) {
        JLabel label = panel.getLabel(name);
        assertEquals(text, label.getText());
    }

    private void assertEmptyField(String name) {
        JTextField field = panel.getField(name);
        assertEquals("", field.getText());
    }

    private void assertEmptyList(String name) {
        JList list = panel.getList(name);
        assertEquals(0, list.getModel().getSize());
    }

    private void assertButtonText(String name, String text) {
        JButton button = panel.getButton(name);
        assertEquals(text, button.getText());
    }
}
